﻿using DL.Tools;
using UnityEngine;
using UnityEngine.AI;

namespace DL.AI.AIBT.Example
{
    public class TreasureAction : IStrategy
    {
        private UnityEngine.GameObject m_Treasure;
        private NavMeshAgent m_Agent;
        private bool m_IsPathCalculated; //是否正在计算路径
        private Transform m_Entity;

        public TreasureAction(GameObject treasure, NavMeshAgent agent, Transform entity)
        {
            m_Treasure = treasure;
            m_Agent = agent;
            m_Entity = entity;
        }

        public Status Process()
        {
            Debugger.EO_Log("m_Treasure.activeSelf : " + m_Treasure.activeSelf);
            if (m_Treasure.activeSelf)
            {
                Debugger.EO_Log("执行策略");
                m_Agent.SetDestination(m_Treasure.transform.position);
                m_Entity.LookAt(m_Treasure.transform);
                
                if (m_IsPathCalculated)
                {
                    m_IsPathCalculated = false;
                }

                if (m_Agent.pathPending)
                {
                    m_IsPathCalculated = true;
                }
            }
            else 
            {
                Debugger.EO_Log("停止移动");
                m_Agent.isStopped = true;
                m_Agent.velocity = Vector3.zero;
            }

            if ( m_Agent.remainingDistance < .1f)
            {
                return Status.Success;
            }
            else
            {
                return Status.Running;
            }
        }

        public void Reset()
        {
            
        }
    }
}